package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.pojo.cards.Cards;
import com.xthink.common.pojo.cards.OperationHistory;
import com.xthink.common.pojo.iPackage.Package;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.utils.OrderNumberUtils;
import com.xthink.common.pojo.cards.CardPackage;
import com.xthink.common.pojo.order.Order;
import com.xthink.common.utils.RedisCache;
import com.xthink.common.vo.cards.BinDingPackageVo;
import com.xthink.common.vo.cards.DetailsPackageVo;
import com.xthink.web.mapper.*;
import com.xthink.web.service.CardPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CardPackageServiceImpl extends ServiceImpl<CardPackageMapper, CardPackage> implements CardPackageService {

    @Autowired
    private CardPackageMapper cardPackageMapper;

    @Autowired
    private CardsMapper cardsMapper;

    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OperationHistoryMapper operationHistoryMapper;

    @Autowired
    private RedisCache redisCache;

    //卡片绑定套餐
    @Override
    public AjaxResult binDingPackage(BinDingPackageVo binDingPackageVo) {
        Integer[] cardsId = binDingPackageVo.getCardsId();
        for (Integer cardId : cardsId) {
            CardPackage cardPackage = new CardPackage();
            cardPackage.setCardId(cardId);
            cardPackage.setPackageId(binDingPackageVo.getPackageId());
            int insert = cardPackageMapper.insert(cardPackage);
            if (insert==0){
                return AjaxResult.error("卡片ID:"+cardId+"绑定套餐失败");
            }
            //生成卡片交易订单
            Order order = new Order();
            order.setOrderNumber(OrderNumberUtils.getTCNumber());
            order.setStatus(2);
            order.setIccId(cardsMapper.selectById(cardId).getIccId());
            order.setPackageName(packageMapper.selectById(binDingPackageVo.getPackageId()).getPackageName() + binDingPackageVo.getMonth());
            order.setTransactionType(0);
            order.setTransactionAmount(binDingPackageVo.getPackagePrice());
            Cards cards = cardsMapper.selectById(cardId);
            Integer userId = cards.getUserId();
            order.setUserId(userId);
            order.setCardId(cardId);
            order.setPackageId(binDingPackageVo.getPackageId());
            orderMapper.insert(order);

            //生成 已购套餐 累充金额  和 销售额 和 佣金额
            LambdaUpdateWrapper<Cards> cardsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            cardsLambdaUpdateWrapper.eq(Cards::getId,cards.getId())
                    //叠加累充金额
                    .set(Cards::getTotalMoney,cards.getTotalMoney() + binDingPackageVo.getPackagePrice())
                    //叠加销售额
                    .set(Cards::getSalePrice,cards.getSalePrice() + binDingPackageVo.getPackagePrice());
            //先提交修改保证销售额达到最大后再统计佣金
            cardsMapper.update(null,cardsLambdaUpdateWrapper);
            //修改成功后再去计算销售额
            LambdaUpdateWrapper<Cards> updateCommission = new LambdaUpdateWrapper<>();
            updateCommission.eq(Cards::getId,cards.getId())
                    //计算用金额
                    .set(Cards::getCommission,cards.getSalePrice() * 0.3);

            //提交修改 更新用金额
            cardsMapper.update(null,updateCommission);

            //记录管理员操作内容
            OperationHistory operationHistory = new OperationHistory();
            operationHistory.setCardId(cardId);
            operationHistory.setName(redisCache.getCacheObject("adminName"));
            operationHistory.setRecord("绑定套餐:"+packageMapper.selectById(binDingPackageVo.getPackageId()).getPackageName());
            operationHistoryMapper.insert(operationHistory);
        }
        return AjaxResult.success();
    }

    //卡片管理解绑套餐 数据回显
    @Override
    public AjaxResult ListBinDingPackage(Integer id) {
        LambdaQueryWrapper<CardPackage> cardPackageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cardPackageLambdaQueryWrapper.eq(CardPackage::getCardId,id);
        List<CardPackage> cardPackages = cardPackageMapper.selectList(cardPackageLambdaQueryWrapper);

        ArrayList<Package> packages = new ArrayList<>();
        for (CardPackage cardPackage : cardPackages) {
            Integer packageId = cardPackage.getPackageId();
            Package aPackage = packageMapper.selectById(packageId);
            packages.add(aPackage);
        }
        return AjaxResult.success(packages);
    }

    //卡片管理中解绑套餐
    @Override
    public AjaxResult unBinDingPackage(Integer cardId, Integer packageId) {
        LambdaQueryWrapper<CardPackage> cardPackageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cardPackageLambdaQueryWrapper.eq(CardPackage::getCardId,cardId);
        cardPackageLambdaQueryWrapper.eq(CardPackage::getPackageId,packageId);
        int delete = cardPackageMapper.delete(cardPackageLambdaQueryWrapper);
        if (delete==0){
            AjaxResult.error("解绑套餐失败");
        }

        //修改订单状态 修改为已结束
        LambdaUpdateWrapper<Order> orderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        orderLambdaUpdateWrapper.eq(Order::getCardId,cardId);
        orderLambdaUpdateWrapper.eq(Order::getPackageId,packageId);
        orderLambdaUpdateWrapper.set(Order::getStatus,3);
        orderMapper.update(null,orderLambdaUpdateWrapper);

        //记录管理员操作内容
        OperationHistory operationHistory = new OperationHistory();
        operationHistory.setCardId(cardId);
        operationHistory.setName(redisCache.getCacheObject("adminName"));
        operationHistory.setRecord("解绑套餐:"+packageMapper.selectById(packageId).getPackageName());
        operationHistoryMapper.insert(operationHistory);
        return AjaxResult.success();
    }

    //卡片详情中的套餐列表
    @Override
    public AjaxResult detailsPackage(DetailsPackageVo detailsPackageVo) {
        LambdaQueryWrapper<CardPackage> cardPackageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cardPackageLambdaQueryWrapper.eq(CardPackage::getCardId,detailsPackageVo.getCardId());
        List<CardPackage> cardPackages = cardPackageMapper.selectList(cardPackageLambdaQueryWrapper);


        ArrayList<Package> packages = new ArrayList<>();
        for (CardPackage cardPackage : cardPackages) {
            Integer packageId = cardPackage.getPackageId();
            LambdaQueryWrapper<Package> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Package::getId,packageId);
            Package aPackage = packageMapper.selectOne(wrapper);
            packages.add(aPackage);
        }

        return AjaxResult.success(packages);
    }
}
